﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Devices;
using Newtonsoft.Json;
using DevicesView.Logic.Test;
using Newtonsoft.Json.Linq;
using DevicesView.Logic;
namespace DevicesView.Logic.Page
{
    public class WebPageScriptLogic
    {
        /// <summary>
        /// 读取本地所有设备与绑定的打印信息
        /// </summary>
        /// <param name="par">可空</param>
        /// <returns></returns>
        public string GetDevices(string par)
        {
            //var devs=DevicesCollection.Devices
            var prints = DevicesCollection.GetCanAddDevices().Select<DevicesInformation, object>(o => new
            {
                dev = o,
                print = Print.PrintLogic.GetPrintConfig(o),
                isAdd = DevicesCollection.Devices == null ? false : DevicesCollection.Devices.Any(d => d.Info.Code == o.Code)
            }).ToList();

            return JsonConvert.SerializeObject(prints);
        }

        /// <summary>
        /// 获取所有打印模板
        /// </summary>
        /// <param name="par">可空</param>
        /// <returns></returns>
        public string GetPrints(string par)
        {
            return JsonConvert.SerializeObject(Print.PrintLogic.GetPrints());
        }

        /// <summary>
        /// 保存打印配置信息
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        public string SavePrintConfig(string par)
        {
            JObject o = JsonConvert.DeserializeObject<JObject>(par);
            DevicesInformation dinfo = o.GetValue("dev").ToObject<DevicesInformation>();
            PrintInfo pinfo = o.GetValue("print").SelectToken("PrintInfo").ToObject<PrintInfo>();
            Print.PrintLogic.SavePrintConfig(dinfo, pinfo);


            //IPrint print = Print.PrintLogic.GetPrintConfig(pinfo);
            //if (print == null)
            //    return false.ToString();
            //Print.PrintLogic.SavePrintConfig(dinfo, print);
            return true.ToString();
        }

        /// <summary>
        /// 调用打印预览
        /// </summary>
        /// <param name="par">IPrint对象JSON</param>
        /// <returns></returns>
        public string PrintViewer(string par)
        {
            //dynamic jobc = JsonConvert.DeserializeObject<dynamic>(par);
            JObject jobj = JsonConvert.DeserializeObject<JObject>(par);
            PrintInfo info = jobj.GetValue("PrintInfo").ToObject<PrintInfo>();

            //PrintInfo info= JsonConvert.DeserializeObject<PrintInfo>(par);
            IPrint print = Print.PrintLogic.GetPrintConfig(info);
            var testresult = new ResultLogic().GetResult();
            List<Devices.Result> result = testresult.Select<Model.ViewData.TestResult, Devices.Result>(o => (Devices.Result)o).ToList();
            print.Preview(result);
            return true.ToString();
        }


        /// <summary>
        /// 获取医院信息
        /// </summary>
        /// <param name="str">可空</param>
        /// <returns></returns>
        public string HospitalInfo(string str)
        {
            return JsonConvert.SerializeObject(App.AppResource.CurrentResource.Hospital);
        }


        /// <summary>
        /// 获取当前页面的趋势信息
        /// </summary>
        /// <param name="par">request ID </param>
        /// <returns></returns>
        public string GetDiagram(string id)
        {
            if (!App.AppResource.Results.ContainsKey(id))
                return null;
            List<Result> results = App.AppResource.Results[id];
            List<PageDataEntity.Diagram> diagrams = new List<PageDataEntity.Diagram>();


            foreach (var item in results[0].ResultDatas)
            {
                diagrams.Add(new PageDataEntity.Diagram
                {
                    Max = item.Max.GetNum(),
                    Min = item.Min.GetNum(),
                    Diaplay = item.Display,
                    ResultDatas = results.Select(r => new PageDataEntity.DiagramItem
                    {
                        Date = r.Date,
                        Value = r.ResultDatas.FirstOrDefault(o => o.Code == item.Code).Value?.ToString().GetNum()
                    }).ToList()
                });
            }

            return JsonConvert.SerializeObject(diagrams);
        }




        public byte[] GetFile(string path)
        {
            return ImageTool.LoadFile(path);
        }



    }
}
